package game.breeze.util;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipUtils {
	private static Map<String, byte[]> entrys = new HashMap<String, byte[]>();

	public static void decompress(InputStream stream) {
		ZipInputStream zipStream = null;
		try {
			if (stream == null || stream.available() <= 0) {
				return;
			}
			zipStream = new ZipInputStream(stream);
			ZipEntry entry = zipStream.getNextEntry();
			while (entry != null) {
				if (!entry.isDirectory()) {

					ByteArrayOutputStream fout = new ByteArrayOutputStream();
					for (int c = zipStream.read(); c != -1; c = zipStream.read()) {
						fout.write(c);
					}
					entrys.put(entry.getName(), fout.toByteArray());
					zipStream.closeEntry();
					fout.close();
				}
				entry = zipStream.getNextEntry();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (zipStream != null) {
				try {
					zipStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {
		// decompress(new
		// FileInputStream("F:\\D\\workspace\\game-breeze\\data\\resource.zip"));
		FileInputStream fin = new FileInputStream("F:\\D\\workspace\\game-breeze\\data\\resource.zip");
		ZipInputStream zin = new ZipInputStream(fin);
		System.out.println(fin.available());
		ZipEntry ze = null;
		while ((ze = zin.getNextEntry()) != null) {
			// if (ze.isDirectory()) {
			// continue;
			// }
			System.out.println("Unzipping " + ze.getName());
			ByteArrayOutputStream fout = new ByteArrayOutputStream();
			for (int c = zin.read(); c != -1; c = zin.read()) {
				fout.write(c);
			}
			System.out.println(fout.toByteArray().length);
			zin.closeEntry();
			fout.close();
		}
		zin.close();
	}

	public static byte[] getEntry(String name) {
		return entrys.get(name);
	}
}
